#+TITLE:Editing Utilities
#+AUTHOR:Joshua Branson
#+LATEX_HEADER: \usepackage{lmodern}
#+LATEX_HEADER: \usepackage[QX]{fontenc}

This files contains some global keybindings and various settings for Emacs.


* lexical binding
  :PROPERTIES:
  :ID:       c901c3d8-a46e-4b21-9105-482ab7b8ef16
  :END:

#+BEGIN_SRC emacs-lisp
;; -*- lexical-binding: t; -*-
#+END_SRC

* Some basic preferences
** default variables
:PROPERTIES:
:ID:       8c2b93d0-e2b4-4a6d-8318-c9036c8df1a2
:END:

Apparently the new Emacs has some quirks with [[gnus:emacs-devel#CAM-tV-9YK-0woZw3thqh7TC_bNb9C=NcpzRdZ7s_NddZWCcrpw@mail.gmail.com][edebug]].  This fixes it.

(fset 'edebug-prin1-to-string #'prin1-to-string)

Alternatively (setq cl-print-readably t) has the same (though it
affects non-edebug usage of cl-print as well).


Make all questions ask you 'y' for yes and 'n' for no instead of having to type "yes" or "no".

#+BEGIN_SRC emacs-lisp
(fset 'yes-or-no-p 'y-or-n-p)
(add-hook 'after-save-hook 'executable-make-buffer-file-executable-if-script-p)
#+END_SRC


#+BEGIN_SRC emacs-lisp
  (setq-default
   ;; It's much easier to move around lines based on how they are displayed, rather than the actual line. This helps a ton with long
   ;; log file lines that may be wrapped:
   line-move-visual t
   visible-bell t
   ring-bell-function 'ignore
   ;;blink-cursor-interval 0.4
   ;; I want to open links from org-mode in chromium
   ;;browse-url-browser-function (quote browse-url-chromium)
   browse-url-browser-function (quote browse-url-firefox)
   bookmark-default-file (expand-file-name ".bookmarks.el" user-emacs-directory)
   bookmark-save-flag 1
   ;;original value was 30.  A large number slows down emacs a bit apparently
   buffers-menu-max-size 20
   case-fold-search t
   default-tab-width 4
   ;; I believe this sets up ediff to split the two windows horizontally AND to NOT show you all the commands
   ;; you can use on the two buffers that you are comapring
   ediff-split-window-function 'split-window-horizontally
   ediff-window-setup-function 'ediff-setup-windows-plain
   ;; When I write org-files, I like to be able to write long lines.
   ;; fill-column 130
   ;;when you are on the last line of the buffer, C-n will act like <return>
   next-line-add-newlines t
   indent-tabs-mode nil
   inhibit-splash-screen t
   inhibit-startup-echo-area-message t
   inhibit-startup-message t
   ;;make-backup-files nil
   initial-scratch-message nil
   ;; don't let the cursor go into minibuffer prompt
   ;; http://ergoemacs.org/emacs/emacs_stop_cursor_enter_prompt.html
   minibuffer-prompt-properties (quote (read-only t point-entered minibuffer-avoid-prompt face minibuffer-prompt))
   save-interprogram-paste-before-kill t
   scroll-preserve-screen-position 'always
   set-mark-command-repeat-pop t
   tooltip-delay 1.5
   truncate-lines nil
   truncate-partial-width-windows nil)
#+END_SRC

#+BEGIN_SRC emacs-lisp
  (line-number-mode 0)
  (column-number-mode 0)
#+END_SRC

I've been having weird clipboard issues with wayland.  So for now, I'm doing to disable x clipboard manager and see if that helps.  Basically, Emacs pauses for a long time when I quit Emacs.

#+BEGIN_SRC emacs-lisp
(setq x-select-enable-clipboard-manager nil)
#+END_SRC


Make TAB mean indent.
(global-set-key "\t" #'indent-for-tab-command)
** saving and backup settings
:PROPERTIES:
:ID:       3dcbac57-2873-48e5-b195-1fc0602c2f52
:END:

For now I am disabling autosave and backup.  This is causing issues
with remote files.  If I find a better way to fix this, I'll do
that...but 'til then I will disable both autosave and backup files.

#+BEGIN_SRC emacs-lisp
;;(setq auto-save-default nil)
;;(setq make-backup-files nil)
#+END_SRC


Emacs' auto-saving probably means something a little different than
what you are used to.  "auto save", means that Emacs will save a
"backup" file for you.  That way if you accidentally delete the file,
you can still have a backup of the file.

Whenever, I change the buffer by 3 characters, go ahead and save the
file.  If Emacs is idle for 5 seconds do auto save.

I should probably try to figure out how to do enable auto-save as
well.  Auto save, lets you save backups of files with the filename
#filename#.

I should probably try to figure out how to do enable auto-save as well.  Auto save, lets you save backups of files with the filename #filename#.

*** autosave
    :PROPERTIES:
    :ID:       318f52d6-8245-40ea-8958-5d78f8a053ff
    :END:
#+BEGIN_SRC emacs-lisp
(defun my-find-file-root-hook ()
  "Some personal preferences."
  ;; Turn auto save off and simplify backups (my version of tramp
  ;; barfs unless I do this:-)
  (setq buffer-auto-save-file-name nil)
  (set (make-local-variable 'backup-by-copying) nil)
  (set (make-local-variable 'backup-directory-alist) '(("."))))

(add-hook 'find-file-root-hook 'my-find-file-root-hook)
#+END_SRC

#+BEGIN_SRC emacs-lisp
    (setq auto-save-file-name-transforms
          `((".*" "~/.config/emacs/emacs-auto-saves/" t)))

    (defun my-create-emacs-auto-saves-directory ()
    (require 'f)
    (when (not (f-directory? "~/.config/emacs/emacs-auto-saves"))
        (f-mkdir "~/.config/emacs/emacs-auto-saves")))

    (add-hook 'after-init-hook 'my-create-emacs-auto-saves-directory)

    (setq auto-save-default nil
            auto-save-interval 20
            auto-save-time-out 5)

    ;; auto saving remote files is wayyy too slow...This should save some time.
      (defun my-create-tramp-auto-saves-directory ()
        (when (not (f-directory? "~/.config/emacs/tramp-auto-saves"))
          (f-mkdir "~/.config/emacs/tramp-auto-saves")))

      (add-hook 'after-init-hook 'my-create-tramp-auto-saves-directory)

      ;; So I am trying this method. Tramp will autosave files in a local directory.
      (setq tramp-auto-save-directory "~/.config/emacs/tramp-auto-saves")

      ;; do not auto save remote file buffers.
      ;; this should work, but it does not...
      ;; the above solution is actually better. tramp-auto-save-directory.
      ;; (defun my/do-not-autosave-remote-buffers ()
      ;;   (when (file-remote-p buffer-file-name)
      ;;     (setq buffer-auto-save-file-name nil)))
      ;; (add-hook 'find-file-hook 'my/do-not-autosave-remote-buffers)

  (setq auto-save-default t
    auto-save-interval 20
    auto-save-time-out 5)
#+END_SRC

Enable auto saving of each file that you are editing.
#+BEGIN_SRC emacs-lisp
(auto-save-visited-mode t)
(setq auto-save-visited-interval 1)
#+END_SRC

*** backup
    :PROPERTIES:
    :ID:       096e1c0c-629d-4ee1-9e4c-16ecf2a68b2f
    :END:
Make Emacs save backup files.  And make it use a special directory for backup files.  It's annoying to have to see a ton of source code files end in "~".
#+BEGIN_SRC emacs-lisp
(setq make-backup-files t
  ;; make emacs save all backup~ files in this directory
  backup-directory-alist '(("" . "~/.config/emacs/backups")))
(add-to-list 'backup-directory-alist
             (cons tramp-file-name-regexp nil))


#+END_SRC

** display line numbers (this is a built in mode)
:PROPERTIES:
:ID:       21945b64-ac1e-4bbc-a67c-b951bd64b9b5
:END:
#+BEGIN_SRC emacs-lisp
(add-hook 'after-init-hook 'global-display-line-numbers-mode)
#+END_SRC
** global keybindings
*** delete and rename current file
:PROPERTIES:
:ID:       29c35b57-c1f6-41da-b72d-be6c6e968f54
:END:
#+BEGIN_SRC emacs-lisp
  (global-set-key (kbd "C-x C-k") #'delete-this-file)
  (global-set-key (kbd "C-x C-r") #'rename-this-file-and-buffer)
#+END_SRC

*** some global keybindings
:PROPERTIES:
:ID:       d760b22b-8cdd-4fb4-9a79-c4aa7a3a127c
:END:
#+BEGIN_SRC emacs-lisp
  (global-set-key (kbd "C-c .") #'xref-find-definitions)
  (global-set-key (kbd "C-c C-h") #'help)
  ;;(global-set-key (kbd "C-c h k") #'helm-show-kill-ring)
  (global-set-key (kbd "C-x f")   #'helm-find-files)
  ;;(global-set-key (kbd "C-c h o") #'helm-occur)
  ;;(global-set-key (kbd "C-c h c") #'helm-calcul-expression)
  ;;(global-set-key (kbd "C-c h g") #'helm-grep-do-git-grep)

#+END_SRC

Transposing words and characters.
#+BEGIN_SRC emacs-lisp
  (global-set-key (kbd "C-c t") #'transpose-chars)
  (global-set-key (kbd "C-c T") #'transpose-words)
#+END_SRC

Helm switch buffers
#+BEGIN_SRC emacs-lisp
(global-set-key (kbd "C-x b") #'helm-buffers-list)
#+END_SRC

*** My specific Gnus startup commands
:PROPERTIES:
:ID:       fd27e689-aab0-4eca-8c0d-4db367c83aea
:END:
I want to defer loading init-gnus.org until I need it.

#+BEGIN_SRC emacs-lisp
  (defun my/gnus ()
    "loads my gnus settings and starts gnus."
    (interactive)
    (org-babel-load-file "~/.config/emacs/lisp/init-gnus.org")
    (gnus))
  (global-set-key (kbd "C-c g") #'my/gnus)
#+END_SRC

#+BEGIN_SRC emacs-lisp
  (defun my/compose-mail ()
    "loads my gnus settings and starts gnus."
    (interactive)
    (org-babel-load-file "~/.config/emacs/lisp/init-gnus.org")
    (compose-mail nil '(("GCC" . "imap.dismail.de:Sent"))))
  (global-set-key (kbd "C-x m") #'my/compose-mail)
#+END_SRC

(listp '((GCC . "imap.dismail.de:Sent")))

*** dired fixed

#+BEGIN_SRC emacs-lisp :tangle no
(global-set-key (kbd "C-x C-j") nil)
#+END_SRC
** My quick commands "C-c <char>"
*** my modkey <letter> commands
:PROPERTIES:
:ID:       f39575b3-9e44-4510-abe0-5120da6ff40f
:END:
My modkey is "command" on a macbook.
#+BEGIN_SRC emacs-lisp

  (global-set-key (kbd "s-;") #'web-mode-comment-or-uncomment)
  (global-set-key (kbd "s-a") #'mark-whole-buffer)
  (global-set-key (kbd "s-d") #'my/downcase-word)
  (global-set-key (kbd "s-g") #'magit-status)
  (global-set-key (kbd "s-h") #'mark-paragraph)
  ;; This causes ERC to connect to the Freenode network upon hitting mod-i
  (global-set-key (kbd "s-i") (lambda () (interactive)
                                (erc :server "irc.freenode.net" :port "6667"
                                     :nick "jbranso")))
#+END_SRC

*** My reserved keychords.  "C-c [a-zA-Z]"
:PROPERTIES:
:ID:       7b79afcf-e0b9-451a-ade1-fbc67d2ff700
:END:
There is only one set of keychords that Emacs reserves for users to set themselves.  That is "C-c <char>".  These are my preferred keychords.

;; this conflicts with my command for dired.
(global-unset-key (kbd "C-a"))
(local-unset-key (kbd "C-a"))

All of my "C-c [letter]" commands
#+BEGIN_SRC emacs-lisp

  (global-set-key (kbd "C-c TAB") #'indent-whole-buffer)
  ;; when point is between two words, delete the space between them
  (global-set-key (kbd "C-c \\") #'delete-horizontal-space)
  (global-set-key (kbd "C-c SPC") #'just-one-space)
  (global-set-key (kbd "C-c $") #'org-archive-subtree)
  (with-eval-after-load 'flyspell
    (define-key flyspell-mode-map (kbd "C-c $") nil))
  ;; some modes my default / in normal mode is NOT bound to helm-swoop, BUT I REALLY LIKE helm-swoop
  (global-set-key (kbd "C-c /") #'helm-swoop)
  (global-set-key (kbd "C-c C-o") #'org-open-at-point-global)
  ;; this is bound in init-smart-comment.el
  ;;(global-set-key (kbd "C-c ;") #'comment-dwim)
  ;;(define-key global-map (kbd "C-c a") 'org-agenda)

  (global-set-key (kbd "C-c a") 'org-agenda)
  (global-set-key (kbd "C-c b") #'(lambda ()
                                    "Switch to the previous buffer"
                                    (interactive)
                                    (switch-to-buffer nil)))
  (global-set-key (kbd "C-c B") #'browse-kill-ring)
  (global-set-key (kbd "C-c C") #'hydra-org-timer/body)
  ;;org-capture is SOOO helpful!!
  (global-set-key (kbd "C-c c") 'org-capture)
  ;;open up a new dired window for the current directory
  (global-set-key (kbd "C-c d") #'dired-jump)
  ;; find the current tag smart.  Just know what I mean.
  ;; For example, with point on the following javascript function upcaseWord
  ;; var upcase =  upcaseWord (string);
  ;; And you type C-c D,  ggtags, when open the buffer where that function is defined
  ;;(global-set-key (kbd "C-c D") 'ggtags-find-tag-dwim)

  (global-set-key (kbd "C-c D") #'dumb-jump-go)
  (global-set-key (kbd "C-c e") #'helm-M-x)
  (global-set-key (kbd "C-c E") #'eshell)
  (global-set-key (kbd "C-c f") #'isearch-forward-regexp)
  (global-set-key (kbd "C-c F") #'isearch-backward-regexp)
  ;; open up email mail program

  (global-set-key (kbd "C-c h") #'helm-command-prefix)
  (global-set-key (kbd "C-c i") #'info-display-manual)
  ;; type this with point at the end of an elisp expression like
  ;; (print 5)<point>
  ;; the result will be 5 printed in the minibuffer
  (global-set-key (kbd "C-c l") #'eval-last-sexp)
  ;;this lets you store an org link from pretty much any file
  ;;then type C-c C-l in an org buffer and it'll put that link it
  (global-set-key (kbd "C-c L") #'org-store-link)
  (global-set-key (kbd "C-c I") #'org-insert-link)
  (global-set-key (kbd "C-c m") #'helm-mini)

  ;;print the working directory in the minibuffer
  ;; I should make these commands copy the output of pwd into the clipboard
  (global-set-key (kbd "C-c P") #'pwd)
  ;;(global-set-key (kbd "C-c p") #'pwd)
  ;;This does recursive find and replace.  But I think it only works when you are in a dired buffer
  (global-set-key (kbd "C-c R") #'find-name-dired)


#+END_SRC
** debugging and visual line mode
   :PROPERTIES:
   :ID:       1d3474a6-f122-4a06-8edc-47711d3b811a
   :END:

When attempting to debug a error, often the error is Soho long that I cannot read it on Emacs.  It makes more sense to turn on visual line mode, so that I can read the whole line.

#+BEGIN_SRC emacs-lisp
  (add-hook 'debugger-mode-hook 'visual-line-mode)
#+END_SRC

** force emacs to use utf8 in all possible situations
:PROPERTIES:
:ID:       2aafacc4-bc8a-4683-a1d3-63cce3f72f84
:END:
 https://thraxys.wordpress.com/2016/01/13/utf-8-in-emacs-everywhere-forever/

 #+BEGIN_SRC emacs-lisp
   (setq locale-coding-system 'utf-8)
   (set-terminal-coding-system 'utf-8)
   (set-keyboard-coding-system 'utf-8)
   (set-selection-coding-system 'utf-8)
   (prefer-coding-system 'utf-8)
   (when (display-graphic-p)
      (setq x-select-request-type '(UTF8_STRING COMPOUND_TEXT TEXT STRING)))
 #+END_SRC
** Making backup files
Diskspace is cheap and making emacs backup my files is probably a good idea maybe this will work.
;; https://www.reddit.com/r/emacs/comments/4398wl/this_is_driving_me_nuts_emacs_will_not_let_me/
;; https://stackoverflow.com/questions/15302973/emacs-auto-save-why-are-files-not-stored-in-the-correct-folder

;; (defvar my-auto-save-folder (concat "~/.config/emacs/auto-save"))  ; folder for auto-saves
;; (setq auto-save-list-file-prefix "~/.config/emacs/auto-save/.saves-")  ; set prefix for auto-saves
;; (setq auto-save-file-name-transforms `((".*", my-auto-save-folder t)))  ; location for all auto-save files
;; (setq tramp-auto-save-directory my-auto-save-folder)  ; auto-save tramp files in local directory

** Whitespace
:PROPERTIES:
:ID:       76a8b375-22f8-4112-983f-c06914a6a1ce
:END:

#+BEGIN_SRC emacs-lisp
  (defun sanityinc/no-trailing-whitespace ()
    "Turn off display of trailing whitespace in this buffer."
    (setq show-trailing-whitespace nil))
#+END_SRC


But don't show trailing whitespace in SQLi, inf-ruby etc.

#+BEGIN_SRC emacs-lisp
  (dolist (hook '(special-mode-hook
                  eww-mode-hook
                  term-mode-hook
                  comint-mode-hook
                  compilation-mode-hook
                  twittering-mode-hook
                  minibuffer-setup-hook))
    (add-hook hook #'sanityinc/no-trailing-whitespace))
#+END_SRC

** Newline behaviour
:PROPERTIES:
:ID:       48ac749b-4186-4069-8182-478a0eae40c3
:END:
#+BEGIN_SRC emacs-lisp
  (global-set-key (kbd "RET") #'newline-and-indent)
#+END_SRC
** Making the default macro better  C-x Q
:PROPERTIES:
:ID:       7dc7b53c-de3e-460c-92b1-62ebb1aee20d
:END:

 When you define a macro, you can type C-x Q to prompt the user for input.
 Very helpful and cool!
  #+BEGIN_SRC emacs-lisp

    (defun my-macro-query (arg)
      "Prompt for input using minibuffer during kbd macro execution.
    With prefix argument, allows you to select what prompt string to use.
    If the input is non-empty, it is inserted at point."
      (interactive "P")
      (let* ((query (lambda () (kbd-macro-query t)))
             (prompt (if arg (read-from-minibuffer "PROMPT: ") "Input: "))
             (input (unwind-protect
                        (progn
                          (add-hook 'minibuffer-setup-hook query)
                          (read-from-minibuffer prompt))
                      (remove-hook 'minibuffer-setup-hook query))))
        (unless (string= "" input) (insert input))))

    (global-set-key "\C-xQ" #'my-macro-query)
 #+END_SRC

** Completition stuff
:PROPERTIES:
:ID:       eec5cf0d-82b8-4642-a1ea-21ac7de754dd
:END:
 Make emacs completetion better
   #+BEGIN_SRC emacs-lisp
     (setq read-file-name-completion-ignore-case t)
     (setq read-buffer-completion-ignore-case t)
 #+END_SRC

add to the list of file names NOT to complete

#+BEGIN_SRC emacs-lisp
  (mapc (lambda (x)
          (add-to-list 'completion-ignored-extensions x))
        '(".aux" ".bbl" ".blg" ".exe"
          ".log" ".meta" ".out"
          ".synctex.gz" ".tdo" ".toc"
          "-pkg.el" "-autoloads.el"
          "Notes.bib" "auto/"))
#+END_SRC

** COMMENT save all buffers after saving the current buffer.
:PROPERTIES:
:ID:       24a0d075-a63a-44b1-be8b-560834b85145
:END:

This may not be necessary anymore.  Emacs is saving all my files automatically.

TODO FIX ME, this is now causing errors?
#+BEGIN_SRC emacs-lisp  :tangle no
  (defun my/save-all-buffers ()
    (interactive)
    (save-some-buffers t))

  (add-hook 'after-save-hook 'my/save-all-buffers)
  (remove-hook 'after-save-hook' my/save-all-buffers)
#+END_SRC

** Autorevert
:PROPERTIES:
:ID:       5361bf81-5d82-4e45-8534-fadd8e575b76
:END:
If a file has changed on disk, then automatically revert the buffer and don't complain about it
#+BEGIN_SRC emacs-lisp
  (global-auto-revert-mode 1)
#+END_SRC

Be quiet about reverting files.

#+BEGIN_SRC emacs-lisp
  (setq auto-revert-verbose nil)
#+END_SRC

This apparently also updates dired buffers too.
#+BEGIN_SRC emacs-lisp
  (setq global-auto-revert-non-file-buffers t)
#+END_SRC
** auto-opening files owned as root, as root.
:PROPERTIES:
:ID:       0e0680c9-a531-49bf-9c98-7a825b2c8819
:END:

Sometimes I want to edit a file like /etc/nginx/nginx.conf.

This file is owned by root.  So when Emacs visits the buffer, I can't edit or save the file.  To fix this problem, I have to open the file via tramp like this:  "/sudo:root@parabola:/etc/nginx/nginx.conf"  or "/sudo::/etc/nginx/nginx.conf".

BUT I want Emacs to check to see if a file is writeable, if it is, then open that file as root.  I can do this by using the find-file-hook!

#+BEGIN_SRC emacs-lisp
  (defun my/find-file-hook ()
    ;; when I can't write the file, open this file as root instead.
    (when (not (file-writable-p buffer-file-name))
      ;; If this file is located on the remote linode machine,
      (if (s-contains? "69.164.207.104" buffer-file-name)
          ;; If this is on the remote machine filename looks like /ssh:joshua@REMOTE:/path/to/file
          ;; I just need to replace "joshua" with "root"
          (find-alternate-file (s-replace "joshua" "root" buffer-file-name))
        (find-alternate-file (concat "/sudo:root@localhost:" buffer-file-name)))))

  (add-hook 'find-file-hook 'my/find-file-hook)
#+END_SRC

** Enabling Emacs commands
*** Enabling narrowing commands
:PROPERTIES:
:ID:       6606c3be-6e09-461a-9fea-34d4369c4ebb
:END:

(put 'narrow-to-region 'disabled nil)
(put 'narrow-to-page 'disabled nil)
(put 'narrow-to-defun 'disabled nil)

Also the default narrow commands suck.  Narrow-dwim is super awesome!

http://endlessparentheses.com/emacs-narrow-or-widen-dwim.html
#+BEGIN_SRC emacs-lisp
(defun narrow-or-widen-dwim (p)
  "Widen if buffer is narrowed, narrow-dwim otherwise.
Dwim means: region, org-src-block, org-subtree, or defun,
whichever applies first. Narrowing to org-src-block actually
calls `org-edit-src-code'.

With prefix P, don't widen, just narrow even if buffer is
already narrowed."
  (interactive "P")
  (declare (interactive-only))
  (cond ((and (buffer-narrowed-p) (not p)) (widen))
        ((region-active-p)
         (narrow-to-region (region-beginning) (region-end)))
        ((derived-mode-p 'org-mode)
         ;; `org-edit-src-code' is not a real narrowing
         ;; command. Remove this first conditional if you
         ;; don't want it.
         (cond ((ignore-errors (org-edit-src-code))
                (delete-other-windows))
               ((ignore-errors (org-narrow-to-block) t))
               (t (org-narrow-to-subtree))))
        ((derived-mode-p 'latex-mode)
         (LaTeX-narrow-to-environment))
        (t (narrow-to-defun))))

;; This line actually replaces Emacs' entire narrowing
;; keymap, that's how much I like this command. Only copy it
;; if that's what you want.
(define-key ctl-x-map "n" #'narrow-or-widen-dwim)
#+END_SRC

*** Enable upcase and downcase
:PROPERTIES:
:ID:       6f046e23-81ba-43d4-af2a-75f37e9ca176
:END:
#+BEGIN_SRC emacs-lisp
  (put 'upcase-region 'disabled nil)
  (put 'downcase-region 'disabled nil)
#+END_SRC

** Delete trailing whitespace on save
:PROPERTIES:
:ID:       6318425f-8bd0-4d44-8d10-f0b40754aab8
:END:

Delete any trailing whitespace before save.
#+BEGIN_SRC emacs-lisp
  (defun my/delete-trailing-whitespace ()
    "This is just a defined function that deletes trailing whitespace"
    (interactive)
    (when
        ;; don't delete white space in my programming dirs
        ;; where I may commit these changes to other projects.
        (and
         (not (s-match ".*prog/guix.*" (buffer-file-name)))
         (not (s-match ".*prog/c.*" (buffer-file-name)))
         (not (s-match ".*prog/gnu.*" (buffer-file-name)))
         (not (s-match ".*prog/web.*" (buffer-file-name))))
      (delete-trailing-whitespace)))

  (defun my/leave-trailing-whitespace-hook  ()
    "This defun leaves trailing whitespace"
    (interactive)
    (remove-hook 'before-save-hook 'my/delete-trailing-whitespace))

  (defun my/delete-trailing-whitespace-hook  ()
    "This defun leaves trailing whitespace"
    (interactive)
    (add-hook 'before-save-hook 'my/delete-trailing-whitespace))

  (my/delete-trailing-whitespace-hook)
#+END_SRC

** Make Emacs automagically fill the paragraph for you as you type
:PROPERTIES:
:ID:       7c64e9ad-bb53-4549-bc29-be947fd2d321
:END:
  Make the lines in the buffer wrap around the edges of the screen.
  YES!!!!! These next two modes auto-indents org-buffers as you type!
  NO NEED FOR to press C-c q or fill-paragraph ever again!

  snaheu snth antuh ah asntuh ntaeo husnth eosntuh antu hsntea
  husntahu l.,rc santhu sntahu src., husanthusant husntah usnat hsnate
  hsantu hsantuh seoantua ou santhue sntahue stnhe snato sntao sntoae
  husntaoh usaneotu h snth santue hstao hsnth sntaho snt hsnta usnth
  sntheoa usnt hsntoh s4cu hsnta husnatu ehs anteh asnteouh snta hunth
  usntha unsto utneo untse hsntea husna hulrc,' huasnthu snato hulcr.,
  husnteahu snahu sanhu snatu hasnu l.,rcuh ansethu aontu hrc, huasu
  hsante neou

#+BEGIN_SRC emacs-lisp
  (defun my/auto-call-fill-paragraph-for-org-mode ()
    "Call two modes to automatically call fill-paragraph for you."
    (visual-line-mode)
    ;;(org-indent-mode)
    )

  (add-hook 'org-mode-hook 'my/auto-call-fill-paragraph-for-org-mode)

  (defun my/auto-fill-mode-and-aggressive-fill-paragraph ()
    "Turn on auto-fill-mode and aggressive-fill-paragraph.  These
      two modes working together will enable you to add text in the
      middle of a paragraph, and Emacs will
      [[info:emacs#Filling][info:emacs#Filling]] auto-fill the
      paragraph for you."
    (auto-fill-mode)
    (aggressive-fill-paragraph-mode))

  ;; aggressive fill paragraph mode does not work for many many many
  ;; major-modes. (dolist (hook '(org-mode-hook prog-mode-hook
  ;; text-mode-hook)) (add-hook hook
  ;; 'my/auto-fill-mode-and-aggressive-fill-paragraph))

#+END_SRC
* Indent whole buffer
:PROPERTIES:
:ID:       98342434-54b9-41d3-bdb4-f1d847d1e7d8
:END:
#+BEGIN_SRC emacs-lisp
(defun indent-whole-buffer ()
  "This indents the whole buffer"
  (interactive)
  (indent-region (point-min) (point-max)))
#+END_SRC

* Making isearch better
:PROPERTIES:
:ID:       19865e0d-3fda-4bcc-93c0-7d35d451125d
:END:
the default behavior on i-search stinks.  This is a lot better
http://endlessparentheses.com/better-backspace-during-isearch.html?source=rss

#+BEGIN_SRC emacs-lisp

(defun mydelete ()
  "Delete the failed portion of the search string, or the last char if successful."
  (interactive)
  (with-isearch-suspended
   (setq isearch-new-string
         (substring
          isearch-string 0 (or (isearch-fail-pos) (1- (length isearch-string))))
         isearch-new-message
         (mapconcat 'isearch-text-char-description isearch-new-string ""))))

(define-key isearch-mode-map (kbd "DEL") 'mydelete)
#+END_SRC

* My hacky autocorrect functionality.
:PROPERTIES:
:ID:       81d7cbf5-13bd-47da-b651-6efacd043bbe
:END:
http://endlessparentheses.com/ispell-and-abbrev-the-perfect-auto-correct.html
I am not a fantastic typist. My speed is acceptable, but I make a great deal of mistakes. The following snippet has turned me into the Messi of keyboards.

Whenever I make a typo:

Hit C-x C-i, instead of erasing the mistake;
Select the appropriate correction (thanks to Ispell);
Sleep easier at night knowing I'll never see that mistake again (thanks to abbrev).

#+BEGIN_SRC emacs-lisp
(define-key ctl-x-map "\C-i" #'endless/ispell-word-then-abbrev)
#+END_SRC

#+BEGIN_SRC emacs-lisp
(global-set-key (kbd "C-c $") #'endless/ispell-word-then-abbrev)
#+END_SRC

#+BEGIN_SRC emacs-lisp
(defun endless/ispell-word-then-abbrev (p)
  "Call `ispell-word', then create an abbrev for it.
With prefix P, create local abbrev. Otherwise it will
be global."
  (interactive "P")
  (let (bef aft)
    (save-excursion
      (while (progn
               (backward-word)
               (and (setq bef (thing-at-point 'word))
                    (not (ispell-word nil 'quiet)))))
      (setq aft (thing-at-point 'word)))
    (when (and aft bef (not (equal aft bef)))
      (setq aft (downcase aft))
      (setq bef (downcase bef))
      (define-abbrev
        (if p local-abbrev-table global-abbrev-table)
        bef aft)
      (message "\"%s\" now expands to \"%s\" %sally"
               bef aft (if p "loc" "glob")))))
#+END_SRC

#+BEGIN_SRC emacs-lisp

(setq save-abbrevs 'silently)
(setq-default abbrev-mode t)
#+END_SRC
* align regexp
:PROPERTIES:
:ID:       be773556-b458-48ec-9e90-38cf9a78a848
:END:
This command is awesome! It'll let you transform stuff like:

var 5 = 10;
var this = 20;
var howIMetYourMother = 29;

var 5                 = 10;
var this              = 20;
var howIMetYourMother = 29;

 By just pressing C-c x RET = RET
 #+BEGIN_SRC emacs-lisp
(global-set-key (kbd "C-c x") #'align-regexp)
 #+END_SRC

* COMMENT Functionality that I really don't use
** I can't get nlinum to work well, so I'm turning it off.
;;snumber of lines you are on. nlinum is much better than linum mode.
;; de make emacs really SLOW when your files get to be past 1000 lines long
;; s faster than linum mode, BUT it will not let me open a new frame
;; kage nlinum
;; e t
;; (global-nlinum-mode 1))
;; hlights search and replacements as you type  VERY helpful for dired-do-replace-regexp and isearch-regexp

** highlight all symbols that match the symbol under point.  Not really useful.
;; ntic is supposed to have that feature too.
;;T highlights the current word under point! very cool!
;; e-package 'highlight-symbol)
;; (hook '(prog-mode-hook html-mode-hook css-mode-hook))
;; ook hook 'highlight-symbol-mode)
;; k hook 'highlight-symbol-nav-mode))
;; ter-load 'highlight-symbol

** I haven't gotten expand region command to work well.

;;--------------------------------------------------------------------
;; egion
;;--------------------------------------------------------------------
;; ing for this is listed below
;; s not play well with evil
;; rarely use it, let's not include it
;; kage expand-region)

** helm ag search
#+BEGIN_SRC emacs-lisp
(global-set-key (kbd "C-c s") #'helm-do-grep-ag)
#+END_SRC

** sx.el stackexchange in emacs

This is the sx.el program, which lets your read, comment, or write stack overflow questions, which is a popular hacking
hele.

(global-set-key (kbd "C-c S") #'sx-search)

** multiple cursors
Mulrsors, which does not work well with evil mode. switch to emacs state to use these commands
(use-package multiple-cursors
  )
(gl-key (kbd "C-c <")   #'mc/mark-previous-like-this) ;
(gl-key (kbd "C-c >")   #'mc/mark-next-like-this)
(gl-key (kbd "C-c C-<") #'mc/mark-all-like-this)
;; ive region to multiple cursors:
;;(et-key (kbd "C-c c c") #'mc/edit-lines)
;;(et-key (kbd "C-c c e") #'mc/edit-ends-of-lines)
;;(et-key (kbd "C-c c a") #'mc/edit-beginnings-of-lines)

;; myself use C-w h/t/n/s when changing to other windows
(gl-key (kbd "C-x o") 'other-window)
;; elf use "s-s"
(glet-key (kbd "C-x C-s"))
** some old stuff that I don't really use. that I didn't write
;;----------------------------------------------------------------------------
;; Fix backward-up-list to understand quotes, see http://bit.ly/h7mdIL
;;----------------------------------------------------------------------------
(defun backward-up-sexp (arg)
  "Jump up to the start of the ARG'th enclosing sexp."
  (interactive "p")
  (let ((ppss (syntax-ppss)))
    (cond ((elt ppss 3)
           (goto-char (elt ppss 8))
           (backward-up-sexp (1- arg)))
          ((backward-up-list arg)))))

(global-set-key [remap backward-up-list] 'backward-up-sexp) ; C-M-u, C-M-up

** A hydra for Rectangle commands

#+BEGIN_SRC emacs-lisp
(defhydra hydra-rectangle (:body-pre (rectangle-mark-mode 1)
                                     :color pink
                                     :post (deactivate-mark))
  "
  ^_t_^     _d_elete    str_i_ng
_n_   _s_   _o_k        _y_ank
  ^_h_^     _n_ew-copy  _r_eset
^^^^        _e_xchange  _u_ndo
^^^^        ^ ^         _p_aste
"
  ("n" backward-char nil)
  ("s" forward-char nil)
  ("t" previous-line nil)
  ("h" next-line nil)
  ("e" exchange-point-and-mark nil)
  ("k" copy-rectangle-as-kill nil)
  ("d" delete-rectangle nil)
  ("r" (if (region-active-p)
           (deactivate-mark)
         (rectangle-mark-mode 1)) nil)
  ("y" yank-rectangle nil)
  ("u" undo nil)
  ("i" string-rectangle nil)
  ("p" kill-rectangle nil)
  ("o" nil nil))

(global-set-key (kbd "C-x SPC") 'hydra-rectangle/body)
#+END_SRC

** move lines up or down
:PROPERTIES:
:ID:       c36069c9-eb4c-40a8-ae82-38f164df1d6c
:END:

This is a cool feature, but I can't find a convenient binding for it.  This would be great for a hydra.

Shift lines up and down with M-up and M-down. When paredit is enabled,
it will use those keybindings. For this reason, you might prefer to
use M-S-up and M-S-down, which will work even in lisp modes.

#+BEGIN_SRC emacs-lisp
(use-package move-dup )
(global-set-key (kbd "M-t") #'md/move-lines-up)
(define-key evil-normal-state-map (kbd "C-s-t") 'md/move-lines-up)
;; this won't work because this is a command that feeds into awesome
(global-set-key (kbd "M-h") #'md/move-lines-down)



(global-set-key (kbd "M-p") 'md/duplicate-down)
(global-set-key (kbd "M-P") 'md/duplicate-up)
#+END_SRC

** COMMENT start the emacs server
:PROPERTIES:
:ID:       84e0b54d-e005-48a8-b940-954da1f944f5
:END:
Start the emacs server for use via org-protocal.
#+BEGIN_SRC emacs-lisp :tangle no
  (require 'server)
  (when (not (server-running-p))
    (server-start))
#+END_SRC
* provide this file
:PROPERTIES:
:ID:       563bf05e-0f8a-4582-95a6-d092a190c95b
:END:
#+BEGIN_SRC emacs-lisp
(provide 'init-editing-utils)
#+END_SRC
